Play 2.4 您所在的位置:网站首页 instanceof Play 2.4

Play 2.4

2023-03-18 16:37| 来源: 网络整理| 查看: 265

How to create an Actor in Play 2.4 so that it automatically injects the dependencies using Guice? I read a couple of posts where they have used GuiceApplicationBuilder and then create actors. I don't want to control how the play application in instantiated. Instead I just want to know how an actor instance can be created so that the dependencies are managed using Guice.

Create a RoundRobinRouter with 5 workers

class RouterActor extends Actor with ActorLogging { val router = { val routees = Vector.fill(5) { val r = context.actorOf(Props[Worker]) context watch r ActorRefRoutee(r) } Router(RoundRobinRoutingLogic(), routees) } def receive = { case job: Job => router.route(job, sender()) } }

Extend AkkaGuiceSupport and create a module where the RouterActor is bound

class RouterActorModule extends AbstractModule with AkkaGuiceSupport { override def configure() = { bindActor[RouterActor]("router-manager") } }

Inject the RouterActor using guice DI based on the name of the actor in the RouterModule

class ScheduledReportGenerationService @Inject() (@Named("router-manager") serviceRouter: ActorRef) extends Actor { def receive = { case serviceInfo: ServiceContext => submitJobs(serviceInfo) } def submitJobs(serviceInfo: ServiceContext) = { serviceRouter ? serviceInfo } }

How to initialize the ScheduledReportGenerationService actor so that Guice automatically injects the RouterActor? I get the following error

java.lang.IllegalArgumentException: no matching constructor found on class vistoscheduler.ScheduledReportGenerationService for arguments []

I understand system.actorOf uses Akka to create the Actor and hence Guice cannot inject the dependency. The documentation is not so clear that I could not get this resolved.

class SchedulerBootStrap extends Actor { implicit val system = context.system implicit val timeout = Timeout(5.minute) def receive = { case "BOOTSTRAP_SCHEDULER" => bootStrapServices() } def bootStrapServices() = { lazy val scheduledReportGenerationService = system.actorOf(Props[ScheduledReportGenerationService], "scheduled-reports-service-actor") scheduledReportGenerationService ? ServiceContext(1L) } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有